package com.example.shengyin.controller;


import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.City;
import com.example.shengyin.entity.Page;
import com.example.shengyin.service.CityService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * AdminController
 *
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/city")
@Validated
//@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("管理员")
//redis缓存用
public class CityController
{
    @Autowired
    private CityService service;

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("传id 0显示全部省，传省的id得到市，传市的id得到区")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable(value = "id")Long id, HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        List<City> allProvince = service.selectById(id);
        System.out.println("===================="+allProvince);
        if (allProvince==null||allProvince.size()==0)
            return Result.error(null);
        return Result.success(allProvince);
    }


    @ApiOperation("传当前name获得当前id")
    @GetMapping("/selectIdByName/{name}")
    public Result selectIdByName(@PathVariable(value = "name") String name, HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        Long aLong = service.selectIdByName(name);
        return Result.success(aLong);
    }

    //根据id查id所对应的省份名
    @ApiOperation("传id查id所对应的省份名")
    @GetMapping("/selectParentNameById/{id}")
    public Result selectParentNameById(@PathVariable(value = "id")Long id, HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }



        String name = service.selectParentNameById(id);

        return Result.success(name,"成功获得name");
    }


    @ApiOperation("传parentid查上级的name")
    @GetMapping("/selectIdByName/{parentid}")
    public Result selectNameByParentid(@PathVariable(value = "parentid")Long parentid, HttpServletRequest request){



        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }



        String parentidname = service.selectNameByParentid(parentid);
        return Result.success(parentidname);
    }



    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result list(Page page,@ApiParam(name = "name",value = "名称") @RequestParam(required = false) String name, HttpServletRequest request)
    {

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }
        List<City> list = service.list(page,name);
        int count = service.count(name);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }
    @ApiOperation("单个查找")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id, HttpServletRequest request)
    {

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }




        City select = service.select(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("添加城市")
    @PostMapping("/add")
    public Result insert(@Valid City city, BindingResult bindingResult, HttpServletRequest request)
    {


        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }




        int insert = service.insert(city);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(@Valid City city, HttpServletRequest request)
    {

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }



        int insert = service.update(city);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
    @ApiOperation("删除单个")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")int id, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }



        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("删除多个")
    @DeleteMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids, HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
}
